package com.ssy.lingxi.system.handler.annotation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 菜单编码source字段校验注解实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2021-01-14
 */
public class MenuCodeSourcValidator implements ConstraintValidator<MenuCodeSourceAnno, Integer> {
    private boolean nullable = true;

    @Override
    public void initialize(MenuCodeSourceAnno constraintAnnotation) {
        nullable = constraintAnnotation.nullable();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if(value == null || value.equals(0)) {
            return nullable;
        } else {
            return Stream.of(1, 2, 99).collect(Collectors.toList()).contains(value);
        }
    }
}
